package com.yang.newCustomersTransferBackProcess.service;

import com.yang.newCustomersTransferBackProcess.dao.*;
import com.yang.newCustomersTransferBackProcess.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ToExamineTransferBackService {

    @Autowired
    private ToExamineTransferBackDao toExamineTransferBackDao;

    @Autowired
    private CustomerAssignmentDao customerAssignmentDao;

    @Autowired
    private CirculationDataExtractionDao circulationDataExtractionDao;

    @Autowired
    private NewCustomersTransferBackDao newCustomersTransferBackDao;

    @Autowired
    private TeamSubmissionRestrictionsDao teamSubmissionRestrictionsDao;

    @Autowired
    private MessageDao messageDao;

    @Autowired AdviserDao adviserDao;

    @Transactional
    public Map<String, Object> toExamine(int toExamineTransferBackId, int adviserId){

        Specification<ToExamineTransferBack> backSpecification = new Specification<ToExamineTransferBack>() {

            @Override
            public Predicate toPredicate(Root<ToExamineTransferBack> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Object> id = root.get("id");
                Predicate predicateId = criteriaBuilder.equal(id, toExamineTransferBackId);

                return criteriaBuilder.and(predicateId);
            }
        };
        ToExamineTransferBack back = toExamineTransferBackDao.findOne(backSpecification).get();

        Specification<Adviser> aSpecification = new Specification<Adviser>() {

            @Override
            public Predicate toPredicate(Root<Adviser> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Object> id = root.get("id");
                Predicate predicateId = criteriaBuilder.equal(id, adviserId);

                return criteriaBuilder.and(predicateId);
            }

        };
        Adviser a = adviserDao.findOne(aSpecification).get();

        if (back.getNewCustomersTransferBackStatus() == 1){
            CustomerAssignment customerAssignment = customerAssignmentDao.findCustomerAssignment(back.getAdviserId(), back.getCustomerId());
            if (customerAssignment == null){
                Map<String, Object> msg = new HashMap<String, Object>();
                msg.put("massage", "客户id跟顾问id不匹配");
                msg.put("code", 400);
                return msg;
            }

            CirculationDataExtraction circulationDataExtraction = circulationDataExtractionDao.findCirculationDataExtraction(back.getCustomerId());
            if (circulationDataExtraction == null){
                Map<String, Object> msg = new HashMap<String, Object>();
                msg.put("massage", "不在7日之内的一次流转资料抽取数据中");
                msg.put("code", 400);
                return msg;
            }

//            NewCustomersTransferBack newCustomersTransferBack = newCustomersTransferBackDao.findNewCustomersTransferBack(back.getCustomerId());
//            if (newCustomersTransferBack != null){
//                Map<String, Object> msg = new HashMap<String, Object>();
//                msg.put("massage", "二次分配流转或者重复提交");
//                msg.put("code", 400);
//                return msg;
//            }

            Integer count = newCustomersTransferBackDao.queryNewCustomersTransferBackCount(back.getCustomerId());
            System.out.println("count："+ count);
            TeamSubmissionRestrictions teamSubmissionRestrictions = teamSubmissionRestrictionsDao.findTeamSubmissionRestrictions(back.getAdviserId());
            System.out.println("teamSubmissionRestrictions.getLimitNumber："+teamSubmissionRestrictions.getLimitNumber());
            if (count >= teamSubmissionRestrictions.getLimitNumber()){
                Map<String, Object> msg = new HashMap<String, Object>();
                msg.put("massage", "该部门已达到限制提交次数");
                msg.put("code", 400);
                return msg;
            }

            NewCustomersTransferBack newCustomersTransferBack1 = newCustomersTransferBackDao.queryNewCustomersTransferBackById(back.getNewCustomersTransferBackId());
            newCustomersTransferBack1.setStatus(2);
            newCustomersTransferBackDao.save(newCustomersTransferBack1);

            back.setStatus(2);
            toExamineTransferBackDao.save(back);

            ToExamineTransferBack toExamineTransferBack1 = new ToExamineTransferBack();
            toExamineTransferBack1.setAdviserId(back.getAdviserId());
            toExamineTransferBack1.setAdviserName(back.getAdviserName());
            toExamineTransferBack1.setCustomerId(back.getCustomerId());
            toExamineTransferBack1.setCustomerName(back.getCustomerName());
            toExamineTransferBack1.setNewCustomersTransferBackStatus(2);
            toExamineTransferBack1.setNewCustomersTransferBackId(back.getNewCustomersTransferBackId());
            toExamineTransferBack1.setStatus(1);
            toExamineTransferBackDao.save(toExamineTransferBack1);

            Adviser adviser = adviserDao.findSuperior(adviserId, 3);

            Message message = new Message();
            message.setSenderId(adviserId);
            message.setSenderName(a.getName());
            message.setReceiverId(adviser.getId());
            message.setReceiverName(adviser.getName());
            message.setContext("主管审核通过");
            message.setStatus(1);
            messageDao.save(message);


        }else if (back.getNewCustomersTransferBackStatus() == 2){
            NewCustomersTransferBack newCustomersTransferBack = newCustomersTransferBackDao.queryNewCustomersTransferBackById(back.getNewCustomersTransferBackId());
            newCustomersTransferBack.setStatus(4);
            newCustomersTransferBackDao.save(newCustomersTransferBack);

            back.setStatus(2);
            toExamineTransferBackDao.save(back);

            Message message = new Message();
            message.setSenderId(adviserId);
            message.setSenderName(a.getName());
            message.setReceiverId(back.getAdviserId());
            message.setReceiverName(back.getAdviserName());
            message.setContext("经理审核通过");
            message.setStatus(1);
            messageDao.save(message);
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "审核成功");
        msg.put("code", 200);
        return msg;
    }

    @Transactional
    public Map<String, Object> failedPassAudit(int toExamineTransferBackId, int adviserId){

        Specification<ToExamineTransferBack> backSpecification = new Specification<ToExamineTransferBack>() {

            @Override
            public Predicate toPredicate(Root<ToExamineTransferBack> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Object> id = root.get("id");
                Predicate predicateId = criteriaBuilder.equal(id, toExamineTransferBackId);

                return criteriaBuilder.and(predicateId);
            }
        };
        ToExamineTransferBack back = toExamineTransferBackDao.findOne(backSpecification).get();

        Specification<Adviser> aSpecification = new Specification<Adviser>() {

            @Override
            public Predicate toPredicate(Root<Adviser> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Object> id = root.get("id");
                Predicate predicateId = criteriaBuilder.equal(id, adviserId);

                return criteriaBuilder.and(predicateId);
            }

        };
        Adviser a = adviserDao.findOne(aSpecification).get();

        if (back.getNewCustomersTransferBackStatus() == 1){

            NewCustomersTransferBack newCustomersTransferBack = newCustomersTransferBackDao.queryNewCustomersTransferBackById(back.getNewCustomersTransferBackId());
            newCustomersTransferBack.setStatus(3);
            newCustomersTransferBackDao.save(newCustomersTransferBack);

//            ToExamineTransferBack toExamineTransferBack1 = new ToExamineTransferBack();
//            toExamineTransferBack1.setAdviserId(back.getAdviserId());
//            toExamineTransferBack1.setAdviserName(back.getAdviserName());
//            toExamineTransferBack1.setCustomerId(back.getCustomerId());
//            toExamineTransferBack1.setCustomerName(back.getCustomerName());
//            toExamineTransferBack1.setNewCustomersTransferBackStatus(3);
//            toExamineTransferBack1.setNewCustomersTransferBackId(back.getNewCustomersTransferBackId());
//            toExamineTransferBack1.setStatus(1);
//            toExamineTransferBackDao.save(toExamineTransferBack1);

            back.setStatus(3);
            toExamineTransferBackDao.save(back);

            Message message = new Message();
            message.setSenderId(adviserId);
            message.setSenderName(a.getName());
            message.setReceiverId(back.getAdviserId());
            message.setReceiverName(back.getAdviserName());
            message.setContext("主管审核不通过");
            message.setStatus(1);
            messageDao.save(message);

        }else if (back.getNewCustomersTransferBackStatus() == 2){
            NewCustomersTransferBack newCustomersTransferBack = newCustomersTransferBackDao.queryNewCustomersTransferBackById(back.getNewCustomersTransferBackId());
            newCustomersTransferBack.setStatus(5);
            newCustomersTransferBackDao.save(newCustomersTransferBack);

            back.setStatus(3);
            toExamineTransferBackDao.save(back);

            Message message = new Message();
            message.setSenderId(adviserId);
            message.setSenderName(a.getName());
            message.setReceiverId(back.getAdviserId());
            message.setReceiverName(back.getAdviserName());
            message.setContext("经理审核不通过");
            message.setStatus(1);
            messageDao.save(message);
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "退回成功");
        msg.put("code", 200);
        return msg;
    }

    public List<ToExamineTransferBack> queryToExamineTransferBack(int adviserId){
        Specification<Adviser> aSpecification = new Specification<Adviser>() {

            @Override
            public Predicate toPredicate(Root<Adviser> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Object> id = root.get("id");
                Predicate predicateId = criteriaBuilder.equal(id, adviserId);

                return criteriaBuilder.and(predicateId);
            }

        };
        Adviser a = adviserDao.findOne(aSpecification).get();

        List<ToExamineTransferBack> toExamineTransferBackList = new ArrayList<>();
        if(a.getRoleId() == 2){
            toExamineTransferBackList = toExamineTransferBackDao.findToExamineTransferBackByRole(a.getTeamId()+"%", 1);
        }else if (a.getRoleId() == 3){
            toExamineTransferBackList = toExamineTransferBackDao.findToExamineTransferBackByRole(a.getTeamId()+"%", 2);

//            toExamineTransferBackList = toExamineTransferBackDao.findToExamineTransferBack(a.getId(), a.getTeamId().length()+2, new int[]{2, 3});
        }

        return toExamineTransferBackList;
    }

    @Transactional
    public Map<String, Object> insertNewCustomersTransferBack(int customerId,
             String customerName,
             int adviserId,
             String adviserName){

        NewCustomersTransferBack newCustomersTransferBack = new NewCustomersTransferBack();
        newCustomersTransferBack.setAdviserId(adviserId);
        newCustomersTransferBack.setAdviserName(adviserName);
        newCustomersTransferBack.setCustomerId(customerId);
        newCustomersTransferBack.setCustomerName(customerName);
        newCustomersTransferBack.setStatus(1);
        newCustomersTransferBackDao.save(newCustomersTransferBack);

        ToExamineTransferBack toExamineTransferBack = new ToExamineTransferBack();
        toExamineTransferBack.setAdviserId(adviserId);
        toExamineTransferBack.setAdviserName(adviserName);
        toExamineTransferBack.setCustomerId(customerId);
        toExamineTransferBack.setCustomerName(customerName);
        toExamineTransferBack.setNewCustomersTransferBackStatus(1);
        toExamineTransferBack.setNewCustomersTransferBackId(newCustomersTransferBack.getId());
        toExamineTransferBack.setStatus(1);
        toExamineTransferBackDao.save(toExamineTransferBack);

        Adviser adviser = adviserDao.findSuperior(adviserId, 2);

        Message message = new Message();
        message.setSenderId(adviserId);
        message.setSenderName(adviserName);
        message.setReceiverId(adviser.getId());
        message.setReceiverName(adviser.getName());
        message.setContext("顾问提交");
        message.setStatus(1);
        messageDao.save(message);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "提交成功");
        msg.put("code", 200);
        return msg;
    }

    public Adviser checkLeader(int adviserId, int digit){

        System.out.println("digit： " + digit);

        Adviser adviser = adviserDao.findLeader(adviserId, digit);

        if (adviser == null) {
            adviser = checkLeader(adviserId, digit+2);
        }
        return adviser;
    }
}
